﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;//声明与数据库有关的命名空间
namespace information
{
    public partial class LookData : Form   //partial关键字用于把类拆成几部分，其中Designer.cs也为LookData类的一部分
    {
        public LookData()
        {
            InitializeComponent();   //界面初始化
        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void EditLook_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
     {      //数据库连接
            string ConnectString = "Server=192.168.0.88; Database=information;User id ='sa';pwd=123456";
            SqlConnection WidgetConnection = new SqlConnection(ConnectString);//初始化一个数据库连接
            MessageBox.Show("数据库连接成功");
        
            //数据库查询
            string SelectString = "select name,sex,time from InformationTable";//定义一个数据库查询字符串
            SqlDataAdapter WidgetAdapter = new SqlDataAdapter(SelectString, WidgetConnection);//初始化数据读取器对象
            DataSet WidgetSet = new DataSet();//初始化数据集
            WidgetAdapter.Fill(WidgetSet, "InformationData");//填充数据集，InformationData用于表映射的源表的名称，原表名为InformationTable
            for (int i = 0; i < WidgetSet.Tables["InformationData"].Rows.Count; i++)//循环遍历数据集中的每一行数据
            {
                ListViewItem lv = new ListViewItem(WidgetSet.Tables["InformationData"].Rows[i][0].ToString());//初始化写入主项
                lv.SubItems.Add(WidgetSet.Tables["InformationData"].Rows[i][1].ToString()); //写入子项
                lv.SubItems.Add(WidgetSet.Tables["InformationData"].Rows[i][2].ToString());
                listView1.Items.Add(lv);   //展示数据
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

    }
}
